package com.base.controller.dsky;

import com.base.po.param.NetState;
import com.base.po.voip.SbdDetail;
import com.base.po.voip.TxSbdIpf;
import com.base.service.SbdDetailService;
import com.base.service.SubscribersService;
import com.base.service.TxSbdIpfService;
import com.base.utils.DateUtil;
import com.base.utils.MapUtils;
import com.beust.jcommander.internal.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "sbd ipf提现接口")
@Slf4j
@RestController
@RequestMapping("/txSbdIpf")
public class TxIpfSbdController {
    private final TxSbdIpfService txSbdIpfService;
    private final SbdDetailService sbdDetailService;
    private final SubscribersService subscribersService;

    public TxIpfSbdController(TxSbdIpfService txSbdIpfService, SbdDetailService sbdDetailService, SubscribersService subscribersService) {
        this.txSbdIpfService = txSbdIpfService;
        this.sbdDetailService = sbdDetailService;
        this.subscribersService = subscribersService;
    }


    @ApiOperation(value = "ipf或者SBD或者Ipg提现")
    @Transactional
    @PostMapping("/doTx")
    public NetState doTx(@ApiParam(value = "用户ID") @RequestParam String userId, @ApiParam(value = "money") @RequestParam BigDecimal money, @ApiParam(value = "1.sbd ") @RequestParam Integer type, @ApiParam(value = "目标钱包地址") @RequestParam String  txAddress,@ApiParam(value = "发票图片") String invoice) throws Exception {
        List<TxSbdIpf> txSbdIpfList = txSbdIpfService.selectByUserIdAndStatus(userId, type);
        if (txSbdIpfList != null && txSbdIpfList.size() >= 1) {
            return new NetState(NetState.FEAILED, "您已经有一条提现记录");
        }
        BigDecimal remainder;
        String orginAdd = null;
        orginAdd =txAddress;
        switch (type) {
            case 1:
                remainder = money.remainder(new BigDecimal(100));
                if (remainder.compareTo(BigDecimal.ZERO) != 0) {
                    return new NetState(NetState.FEAILED, "SBD提现金额为100的倍数");
                }
                BigDecimal allmoney = sbdDetailService.getSbdDetailMoney(userId, 100);
                if (allmoney.compareTo(money) < 0) return new NetState(NetState.FEAILED, "您的账户余额不足");
                SbdDetail sbdDetail = new SbdDetail(null, userId, "提现", "提现扣除", money, 0, 3, new Date());
                sbdDetailService.insertSelective(sbdDetail);
                break;
            default:
                return new NetState(NetState.FEAILED, "参数传入错误");
        }
        TxSbdIpf txSbdIpf = new TxSbdIpf();
        txSbdIpf.setTx_pack(orginAdd);
        txSbdIpf.setMoney(money);
        txSbdIpf.setStatement(0);
        txSbdIpf.setTx_time(new Date());
        txSbdIpf.setType(type);
        txSbdIpf.setSubId(userId);
        txSbdIpf.setInvoice(invoice);
        txSbdIpfService.insertSelective(txSbdIpf);
        return new NetState("成功", NetState.SUCCESS_CONN, txSbdIpf);
    }

    @ApiOperation(value = "IPF/SBD/IPG提现记录查询")
    @Transactional
    @PostMapping("/searchBy")
    public NetState searchBy(@ApiParam(value = "用户ID") @RequestParam String userId, @ApiParam(value = "页码") @RequestParam Integer pageNo) throws Exception {
        List<TxSbdIpf> txSbdIpfList = txSbdIpfService.searchBy(userId, null, null, null, null, pageNo);
        List<Map<String, Object>> result = Lists.newArrayList();
        txSbdIpfList.forEach(item -> {
            Map<String, Object> map = MapUtils.object2Map(item);
            map.put("date", DateUtil.dateToStrLong(item.getTx_time()));
            result.add(map);
        });
        return new NetState("成功", NetState.SUCCESS_CONN, result);
    }


    @ApiOperation(value = "审核用")
    @Transactional
    @PostMapping("/verify")
    public synchronized NetState verify(@ApiParam(value = "txId") @RequestParam Integer txId, @ApiParam(value = "审核状态") @RequestParam Integer status) throws Exception {
        TxSbdIpf txSbdIpf = txSbdIpfService.selectByPrimaryKey(txId);
        if (txSbdIpf == null) return new NetState(NetState.FEAILED, "无法查询该ID记录");
        if (txSbdIpf.getStatement().equals(1) || txSbdIpf.getStatement().equals(2)) {
            return new NetState(NetState.FEAILED, "该记录已经进入终态");
        }
        switch (status) {
            case 1:
                txSbdIpf.setStatement(status);
                txSbdIpfService.updateByPrimaryKeySelective(txSbdIpf);
                break;
            case 2:
                txSbdIpf.setStatement(status);
                txSbdIpfService.updateByPrimaryKeySelective(txSbdIpf);
                //退钱
                if (txSbdIpf.getType().equals(1)) {
                    SbdDetail sbdDetail = new SbdDetail(null, txSbdIpf.getSubId(), "审核失败退回", "审核失败退回", txSbdIpf.getMoney(), 1, 1, new Date());
                    sbdDetailService.insertSelective(sbdDetail);
                }
                break;
        }
        return new NetState(NetState.SUCCESS_CONN, "审核成功");
    }
}
